/*
 * Copyright (C) Michael Opitz
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA
 */
#ifndef WEB_HANDLER_H
#define WEB_HANDLER_H

#include <boost/weak_ptr.hpp>

#include "../handler/event_handler.h"
#include "../network/connection_manager.h"
#include "../music/music_system.h"

/** 
 * \brief This namespace includes all user-specific handlers for the iRoom.
 * \author Michael Opitz
 */
namespace user_handler {
	class web_handler;
}

/** 
 * \brief This namespace includes all user-specific handlers for the iRoom.
 * \author Michael Opitz
 */
namespace user_handler {
	/** 
	 * \brief This class handles all messages from the web-client.
	 */
	class web_handler : public handler::event_handler {
	public:
		/** 
		 * \brief web_handler creates the web_handler. Never call this
		 * constructor directly, use the create-method instead.
		 * 
		 * \param s is the socket to the web-client
		 * \param micro is the socket to the microcontroller
		 * \param mu is a pointer to the music-system
		 * \param mgr is a pointer to the connection-manager.
		 */
		web_handler(boost::shared_ptr<network::i_socket> s,
			    boost::shared_ptr<network::i_socket> micro,
			    boost::shared_ptr<music::music_system> mu,
			    boost::shared_ptr<network::connection_manager> mgr)
		: _sock(s), _controller(micro), _music(mu), _mgr(mgr) {}
		virtual ~web_handler() {}

		/** 
		 * \brief handle_event handles all received events from a
		 * web-client.
		 * 
		 * \param n is a pointer to the received message.
		 */
		virtual void handle_event(
				boost::shared_ptr<network::message> n);

		/** 
		 * \brief create creates a new web_handler object.
		 * \see web_handler
		 */
		static boost::shared_ptr<user_handler::web_handler> create(
			boost::shared_ptr<network::i_socket> sock, 
			boost::shared_ptr<network::i_socket> control, 
			boost::shared_ptr<music::music_system> music,
			boost::shared_ptr<network::connection_manager> mgr)
		{
			boost::shared_ptr<user_handler::web_handler> ptr(
				new user_handler::web_handler(sock, control, 
					music, mgr));
			ptr->_this = ptr;
			return ptr;
		}

	private:
		void do_play(boost::shared_ptr<model::dom> node);
		void do_list(boost::shared_ptr<model::dom> node);
		void do_pause(boost::shared_ptr<model::dom> node);
		void do_temp(boost::shared_ptr<model::dom> node);
		void do_light(boost::shared_ptr<model::dom> node);
		void do_tv(boost::shared_ptr<model::dom> node);
		void do_random(const string &client, const string &file);

		boost::shared_ptr<network::i_socket> _sock;
		boost::shared_ptr<network::i_socket> _controller; 
		boost::shared_ptr<network::connection_manager> _mgr; 
		boost::shared_ptr<music::music_system> _music;

		boost::weak_ptr<user_handler::web_handler> _this;
	};
}

#endif
